package GUI;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import Executor.ExecutionManager;
import DataStructures.ResultBean;
import DataStructures.ResultsBean;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Manuel
 */
public class RequestReceiver extends HttpServlet {
   
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response){
        System.out.println("#########################################################################################################");
        ExecutionManager executor = new ExecutionManager();
        executor.removeWD();
        HashMap parameters = new HashMap();
        List<String> parameterNames = Collections.list((Enumeration<String>)request.getParameterNames());
        for ( String parameterName : parameterNames){
            parameters.put(parameterName, request.getParameter(parameterName));
        }
        ResultsBean bean = null;
        if(parameters.containsKey("code")){
            if(request.getParameter("filter").equals("true")){
                bean =executor.processFilterRequest(request);
            }
            else
                bean = executor.processResultRequest(request);
        }
        else if(parameters.containsKey("queryfield")) {
            bean = executor.executeRequest((String) parameters.get("queryfield"));
        }
        else{
            bean = executor.executeRequest(parameters);
        }
        request.setAttribute("response", bean);
        RequestDispatcher dispatcher = request.getRequestDispatcher("result.jsp");
        try {
            dispatcher.forward(request, response);
        } catch (ServletException ex) {
            Logger.getLogger(RequestReceiver.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(RequestReceiver.class.getName()).log(Level.SEVERE, null, ex);
        }

    } 

    protected void doPost(HttpServletRequest request, HttpServletResponse response){

    }

}
